﻿#ifndef MYPIC_H
#define MYPIC_H


#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QGraphicsEllipseItem>
#include <QGraphicsSceneMouseEvent>
#include <QDebug>
#include <QObject>
#include "DraggablePoint.h"

class MyPic : public QGraphicsPixmapItem {

public:
    MyPic(const QPixmap &pixmap, QGraphicsItem *parent = nullptr) : QGraphicsPixmapItem(pixmap, parent) {

        setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);



    }
    void addCornerPoint(DraggablePoint* point) {

        cornerPoints.append(point);

    }

protected:
    void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override {
        if (event->buttons() & Qt::LeftButton) {
            // 计算移动的距离
            QPointF newPos = event->scenePos() - event->lastScenePos();
            // 更新图片的位置
            setPos(pos() + newPos);

            updateCornerPositions(newPos);
        } else {
            QGraphicsPixmapItem::mouseMoveEvent(event);
        }
    }

private:
    QList<DraggablePoint*> cornerPoints;
    // 更新四个点的位置
    void updateCornerPositions(const QPointF& newPos) {
        foreach(QGraphicsItem* item, cornerPoints) {
            DraggablePoint* point = qgraphicsitem_cast<DraggablePoint*>(item);
            if (point) {
                point->setPos(point->pos() + newPos);
            }
        }
    }
};

#endif // MYPIC_H
